#include <TStyle.h>
#include <string>
#include <THStack.h>
#include <TH1.h>
#include <TH1F.h>
#include <TAxis.h>
#include <TCanvas.h>
#include <TPad.h>
#include <TFile.h>
#include <TLegend.h>

using namespace std;

void q1D(std::string var, std::string tituloX, std::string tituloY, Double_t ry1, Double_t ry2)
{
  Int_t icol=0;
  gStyle->SetFrameBorderMode(icol);
  gStyle->SetFrameFillColor(icol);
  gStyle->SetCanvasBorderMode(icol);
  gStyle->SetCanvasColor(icol);
  gStyle->SetPadBorderMode(icol);
  gStyle->SetPadColor(icol);
  gStyle->SetStatColor(icol);

//  gStyle->SetPaperSize(26,20);
  gStyle->SetPadTopMargin(0.15);
  gStyle->SetPadRightMargin(0.26);
  gStyle->SetPadBottomMargin(0.16);
  gStyle->SetPadLeftMargin(0.12);

  //gStyle->SetTitleXOffset(1.0);
  //gStyle->SetTitleYOffset(1.0);
  Int_t font=42;
  Double_t tsize=0.05;
  gStyle->SetTextFont(font);
  gStyle->SetTextSize(tsize);
  gStyle->SetLabelFont(font,"x");
  gStyle->SetTitleFont(font,"x");
  gStyle->SetLabelFont(font,"y");
  gStyle->SetTitleFont(font,"y");
  gStyle->SetLabelFont(font,"z");
  gStyle->SetTitleFont(font,"z");
  gStyle->SetLabelSize(tsize,"x");
  gStyle->SetTitleSize(tsize,"x");
  gStyle->SetLabelSize(tsize,"y");
  gStyle->SetTitleSize(tsize,"y");
  gStyle->SetLabelSize(tsize,"z");
  gStyle->SetTitleSize(tsize,"z");
  gStyle->SetMarkerStyle(20);
  gStyle->SetMarkerSize(0.7);
  gStyle->SetHistLineWidth(2);
  gStyle->SetLineStyleString(2,"[12 12]");
  //gStyle->SetEndErrorSize(0.);
  gStyle->SetOptTitle(0);
  gStyle->SetOptStat(111111111);
  //gStyle->SetOptStat(0);
  gStyle->SetOptFit(0);
  gStyle->SetPadTickX(1);
  gStyle->SetPadTickY(1);
  gStyle->SetPalette(1);
  int nfiles=9;
  TFile *f[nfiles];
  f[0] = TFile::Open("../sumbg/plots.root","READ");
  f[1] = TFile::Open("../qcd/plots.root","READ");
  f[2] = TFile::Open("../ttbar/plots.root","READ");
  f[3] = TFile::Open("../dibosons/plots.root","READ");
  f[4] = TFile::Open("../zplusjets/plots.root","READ");
  f[5] = TFile::Open("../wplusjets/plots.root","READ");
  f[6] = TFile::Open("../singletop/plots.root","READ");
  f[7] = TFile::Open("../signal/plots.root","READ");
  f[8] = TFile::Open("../real/plots.root","READ");
  //f[8]= TFile::Open("../../doplotsalaemma/plots.root","READ"); 

  THStack* hstack = new THStack("hs","hstacked histograms");
  TH1F* hsumbg;
  TH1F* hsumbgaux2;
  TH1F* hqcd;
  TH1F* httbar;
  TH1F* hdibosons;
  TH1F* hzplusjets;
  TH1F* hwplusjets;
  TH1F* hsingletop;
  TH1F* hsignal;
  TH1F* hreal;
  TH1F hratiovar;
  TH1F hunovar;


  hsumbg = (TH1F*)f[0]->Get(var.c_str());
  hsumbgaux2 = (TH1F*)f[0]->Get(var.c_str());
  hqcd = (TH1F*)f[1]->Get(var.c_str());
  httbar = (TH1F*)f[2]->Get(var.c_str());
  hdibosons = (TH1F*)f[3]->Get(var.c_str());
  hzplusjets = (TH1F*)f[4]->Get(var.c_str());
  hwplusjets = (TH1F*)f[5]->Get(var.c_str());
  hsingletop = (TH1F*)f[6]->Get(var.c_str());
  hsignal = (TH1F*)f[7]->Get(var.c_str());
  hreal = (TH1F*)f[8]->Get(var.c_str());

  hratiovar = (*hreal) / (*hsumbg);
  hunovar = (*hsumbg) / (*hsumbg);

  hqcd->SetStats(0);
  hdibosons->SetStats(0);
  httbar->SetStats(0);
  hwplusjets->SetStats(0);
  hzplusjets->SetStats(0);
  hsingletop->SetStats(0);
  hsignal->SetStats(0);
  hsumbgaux2->SetStats(0);

 
  std::string tc = "c"+var;
  std::string tp1 = "p1"+var;
  std::string tp2 = "p2"+var;
  TCanvas *cvar = new TCanvas(tc.c_str(),tc.c_str());
  TPad *p1var = new TPad(tp1.c_str(),tp1.c_str(),0,0.3,1,1);
  TPad *p2var = new TPad(tp2.c_str(),tp2.c_str(),0,0,1,0.3);
  p1var->SetFillStyle(4000);
  p1var->SetBottomMargin(0);
  p1var->SetTopMargin(0.05);
  //p2var->SetFillStyle(4000);
  p2var->SetTopMargin(0.07);
  p2var->SetBottomMargin(0.45);
  p1var->Draw();
  p2var->Draw();
  p1var->cd();
  p1var->SetGrid();
  p2var->SetGrid();
  p1var->SetLogy();


  hsignal->SetLineStyle(2);
  hsignal->SetLineWidth(3);
  hsignal->SetLineColor(kViolet);
  hreal->SetMarkerStyle(20);
  hreal->SetMarkerSize(0.9);
  hreal->SetMarkerColor(kBlack);

  hstack->SetMinimum(ry1);
  hstack->SetMaximum(ry2);
  TAxis* yastack = hqcd->GetYaxis();
  yastack->SetTitleSize(0.07);
  yastack->SetTitleOffset(0.75);
  yastack->SetLabelSize(0.07);
  yastack->SetTitle(tituloY.c_str());
  hstack->SetHistogram(hqcd);


  hdibosons->SetFillColor(28);
  hstack->Add(hdibosons);
  hsingletop->SetFillColor(kGreen-5);
  hstack->Add(hsingletop);
  httbar->SetFillColor(kGreen-9);
  hstack->Add(httbar); 
  hzplusjets->SetFillColor(kBlue+3);
  hstack->Add(hzplusjets);
  hwplusjets->SetFillColor(kAzure+1);
  hstack->Add(hwplusjets);
  hqcd->SetFillColor(kWhite);
  hstack->Add(hqcd);

  TH1F* hlast = dynamic_cast<TH1F*>(hstack->GetStack()->Last());
  hlast->SetLineColor(kRed);
  hlast->SetFillStyle(2001);
  hlast->SetFillColor(kYellow);

  hstack->Draw("HIST");
  hlast->Draw("E2HISTSAME");
  hsignal->Draw("HISTSAME");
  hreal->Draw("EPSAMES");

  //hstack.Add();
  p1var->RedrawAxis();
  TLegend* ley1var = new TLegend(.77,.02,.99,.55);
  ley1var->AddEntry(hreal,"data L=35pb^{-1}","P");
  ley1var->AddEntry(hlast,"backgrounds","LF");
  ley1var->AddEntry(hsignal,"BRPV SU3","L");
  ley1var->AddEntry(hqcd,"qcd x0.55","F");
  ley1var->AddEntry(hwplusjets,"W + jets","F");
  ley1var->AddEntry(hzplusjets,"Z + jets","F");
  ley1var->AddEntry(httbar,"ttbar","F");
  ley1var->AddEntry(hsingletop,"single top","F");
  ley1var->AddEntry(hdibosons,"dibosons","F");
  ley1var->SetFillColor(kWhite);
  ley1var->Draw("SAME");
  p1var->Modified();
  cvar->cd();

  //**********************************************************************************************************************
  //****************************************************** second Pad ****************************************************
  //**********************************************************************************************************************
  p2var->cd();
  hratiovar.SetMarkerSize(0.9);
  hratiovar.SetMarkerStyle(20);
  hratiovar.SetStats(0);

  hunovar.SetStats(0);
  hunovar.SetFillColor(kYellow);
  hunovar.SetLineColor(kYellow);
  hsumbgaux2->SetFillColor(kYellow);
  hsumbgaux2->SetFillStyle(1001);
  hsumbgaux2->SetLineColor(kBlack);
  hunovar.SetMarkerColor(kRed);
  hunovar.SetMarkerSize(0.1);
  hunovar.SetFillStyle(2001);
  TAxis *yunovar = hunovar.GetYaxis();
  yunovar->SetNdivisions(204,kFALSE);
  yunovar->SetRangeUser(0,2.0);
  yunovar->SetLabelSize(0.13);
  yunovar->SetLabelOffset(0.01);
  yunovar->SetTitleSize(0.13);
  yunovar->SetTitleOffset(0.4);
  yunovar->SetTitle("Ratio   ");
  TAxis *xunovar = hunovar.GetXaxis();
  xunovar->SetTitleOffset(1.0);
  xunovar->SetTitleSize(0.18);
  xunovar->SetTitle(tituloX.c_str());
  xunovar->SetLabelOffset(0.02);
  xunovar->SetLabelSize(0.15);  

  hunovar.Draw("E2HIST");
  hratiovar.Draw("EPSAME");

  TLegend* ley2var = new TLegend(.79,.50,.98,.90);
  ley2var->AddEntry(&hratiovar,"data/MC","P");
  ley2var->AddEntry(hsumbgaux2,"MC/MC","F");
  ley2var->SetFillColor(kWhite);
  ley2var->Draw("SAME");
  p2var->Modified();
  cvar->cd();
  cvar->SetSelected(cvar);
  std::string argpng = "ploticos/"+var+".png";
  cvar->SaveAs(argpng.c_str());
  for(int i=0; i<nfiles; i++){
    f[i]->Close();
  }
  cvar->Close();

}

